package com.design.pattern.responsibility;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author goujian
 * @description: TODO
 * @date 2022/3/16 2:21 下午
 */
@Slf4j
@Component
public class HandlerExecution implements InitializingBean, ApplicationContextAware {

    private ApplicationContext applicationContext;

    public void accpetRequest(RequestParam requestParam) {
        // 执行责任链
        abstractApproverList.stream()
                .forEach(approver -> {approver.handle(requestParam);});
    }

    // 责任链
    private List<AbstractApprover> abstractApproverList;

    public List<AbstractApprover> getAbstractApproverList() {
        return abstractApproverList;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        // 设置责任链
        abstractApproverList = applicationContext.getBeansOfType(AbstractApprover.class)
                .values().stream().collect(Collectors.toList());
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
